//
//  PersonalHomePageBasseController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PersonalHomePageBasseController.h"
#import "PersonalHomeHeaderView.h"
#import "PersonalHomePageAllController.h"
#import "PersonalHomePageArticleController.h"
#import "PersonalHomePageVideoController.h"
#import "PersonalHomePageSmallVideoController.h"
#import "PersonalHomePageQuestionAnswerController.h"
#import "PersonalHomePageFoodController.h"

#import "MultipleGestureTableView.h"
#import "MultipleScrollViewManager.h"
#import "HFStretchableTableHeaderView.h"

@interface PersonalHomePageBasseController ()<SGPageTitleViewDelegate, SGPageContentScrollViewDelegate,UITableViewDelegate, UITableViewDataSource>

/**管理器对象 这个在主控制器里进行声明*/
@property (strong, nonatomic) MultipleScrollViewManager *kjManager;
/**uitableView 用MultipleGestureTableView是为了多手势支持，也可以不用*/
@property (strong, nonatomic) MultipleGestureTableView *tView;

@property (strong, nonatomic) UIView *kjView;
/**下拉放大的管理器*/
//@property (strong, nonatomic) HFStretchableTableHeaderView *kjHfs;
/**标题视图*/
@property (nonatomic,strong)SGPageTitleView*pageTitleView;
/**初始化标题视图配置文件*/
@property (nonatomic,strong)SGPageTitleViewConfigure *configure;
/**子视图切换容器*/
@property (nonatomic,strong)SGPageContentScrollView*pageContentScrollView;
/**子控制器数组*/
@property (nonatomic,strong)NSArray*childArr;
/**头部视图*/
@property (nonatomic,strong)PersonalHomeHeaderView*headerView;
@end

@implementation PersonalHomePageBasseController


- (void)loadView {
    [super loadView];
    
    [self.view addSubview:self.tView];
    [self.tView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //初始化多视图管理器
    self.kjManager = [MultipleScrollViewManager new];
    //把滚动视图交给管理器
    [self.kjManager addMainView:self.tView];
    
    //把header交给下拉放大管理器
    //下拉放大
//    self.kjHfs = [HFStretchableTableHeaderView new];
//    [self.kjHfs stretchHeaderForTableView:self.tView withView:self.headerView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view layoutIfNeeded];
    self.title = @"我是一个购物狂";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addNavigationItemWithImageNames:@[@"sangedian"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    DLogFunc;
    DLog(@"%@",NSStringFromCGPoint(self.tView.contentOffset));
    DLog(@"%@",NSStringFromCGSize(self.tView.contentSize));
    DLog(@"%@",NSStringFromUIEdgeInsets(self.tView.contentInset));

    self.tView.contentOffset = CGPointMake(0, 0);
    [self.tView setContentOffset:CGPointMake(0, 0) animated:YES];
    DLogFunc;
    DLog(@"%@",NSStringFromCGPoint(self.tView.contentOffset));
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndNotClearColor];

    [self performSelector:@selector(dowmAction) withObject:nil afterDelay:0.5];
    
}
-(void)setType:(PersonalHomePageSwithType)type{
    _type = type;
    self.headerView.type = type;
}
-(void)dowmAction{
    self.kjManager.isOne = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.kjManager.isOne = NO;
}
-(void)rightAction{
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectZero;
    [object showPresentingViewController:self PresentedViewController:[[BasseNavigationController alloc]initWithRootViewController:[ShareViewController new]]];
}
-(void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    [self performSelector:@selector(dowmAction1) withObject:nil afterDelay:0.2];
}

-(void)dowmAction1{
    if (self.selectIndex == 1) {
        self.pageTitleView.resetSelectedIndex = 1;
    }
}
///**由于下拉放大必须实现，如果不需要下拉放大效果，这里可以不需要*/
//- (void)viewDidLayoutSubviews {
//    [self.kjHfs resizeView];
//}
//
///**这里由于下拉放大需要，如果不需要下拉放大效果，则可以不实现*/
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    [self.kjHfs scrollViewDidScroll:scrollView];
//}

/**初始化sectionHeaderView*/
- (void)loadKjView {
    if (!self.kjView) {
        //由于是放到sectionHeader上的 所以这里需要使用frame,一般这里的宽高就是屏幕的宽高，如果要考虑导航栏和tabbar的高度，这里的高度是要减去导航栏高度(64或88)和tabar的高度
        self.kjView = [[UIView alloc] initWithFrame:CGRectMake(0, K_TopHeight, KScreenWidth, KScreenHeight-K_TopHeight)];
        //初始化分页控制器
        [self loadPageView];
    }
}
/**初始化分页控制器(PageViewController)*/
- (void)loadPageView {
    //titleView
    [self.kjView addSubview:self.pageTitleView];
    //contentView
    [self.kjView addSubview:self.pageContentScrollView];
    self.pageTitleView.selectedIndex = 0;
}
#pragma mark - UITableViewDelegate、UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    //这个header的高度就是放置xjView的，所以一般都是当前页面的高度，如果考虑导航栏和tabbar。那么要减去导航栏高度和tabbar的高度
    return KScreenHeight-K_TopHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (!self.kjView) {
        [self loadKjView];
    }
    return self.kjView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
//    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
//    return cell;
}
#pragma mark - SGPageTitleViewDelegate
/**
 *  联动 pageContent 的方法
 *
 *  @param pageTitleView      SGPageTitleView
 *  @param selectedIndex      选中按钮的下标
 */
- (void)pageTitleView:(SGPageTitleView *)pageTitleView
        selectedIndex:(NSInteger)selectedIndex {
    if (selectedIndex != 2) {
        PersonalHomePageVideoController *threeVC  = self.childArr[2];
        [threeVC pausePlay];
    }
    [self.pageContentScrollView setPageContentScrollViewCurrentIndex:selectedIndex];
}

#pragma mark - SGPageContentScrollViewDelegate
/**
 *  联动 SGPageTitleView 的方法
 *
 *  @param pageContentScrollView      SGPageContentScrollView
 *  @param progress                   SGPageContentScrollView 内部视图滚动时的偏移量
 *  @param originalIndex              原始视图所在下标
 *  @param targetIndex                目标视图所在下标
 */
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView
                     progress:(CGFloat)progress
                originalIndex:(NSInteger)originalIndex
                  targetIndex:(NSInteger)targetIndex {
    if (targetIndex != 2) {
        PersonalHomePageVideoController *threeVC  = self.childArr[2];
        [threeVC pausePlay];
    }

    [self.pageTitleView setPageTitleViewWithProgress:progress
                                     originalIndex:originalIndex
                                       targetIndex:targetIndex];
}
#pragma mark 懒加载
-(NSArray*)childArr{
    if(!_childArr){
        PersonalHomePageAllController *oneVC = [[PersonalHomePageAllController alloc] init];
        oneVC.sManager = self.kjManager;
        
        PersonalHomePageArticleController *twoVC = [[PersonalHomePageArticleController alloc] init];
        twoVC.sManager = self.kjManager;

        PersonalHomePageVideoController *threeVC = [[PersonalHomePageVideoController alloc] init];
        threeVC.sManager = self.kjManager;

        PersonalHomePageSmallVideoController *fourVC = [[PersonalHomePageSmallVideoController alloc] init];
        fourVC.sManager = self.kjManager;

        PersonalHomePageQuestionAnswerController *fiveVC = [[PersonalHomePageQuestionAnswerController alloc] init];
        fiveVC.sManager = self.kjManager;

        PersonalHomePageFoodController *sixVC = [[PersonalHomePageFoodController alloc] init];
        sixVC.sManager = self.kjManager;


        _childArr= @[oneVC, twoVC, threeVC, fourVC, fiveVC, sixVC];
    }
    return _childArr;
}

-(PersonalHomeHeaderView*)headerView{
    if(!_headerView){
        _headerView=[[PersonalHomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
        _headerView.backgroundColor = KWhiteColor;
    }
    return _headerView;
}
-(SGPageTitleViewConfigure*)configure{
    if(!_configure){
        _configure=[SGPageTitleViewConfigure pageTitleViewConfigure];
        _configure.titleTextZoom = YES;
        _configure.titleTextZoomRatio = 0.5;
        _configure.titleAdditionalWidth = 30;
        _configure.titleGradientEffect = YES;
        _configure.titleSelectedColor = UIColorFromRGB(0x009966);
        _configure.indicatorColor = UIColorFromRGB(0x009966);
        _configure.titleFont = PingFangSC_Medium(16);
//        _configure.titleSelectedFont = SYSTEMFONT(22);
    }
    return _configure;
}


-(MultipleGestureTableView*)tView{
    if(!_tView){
        _tView=[[MultipleGestureTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tView.delegate = self;
        _tView.dataSource = self;
        _tView.backgroundColor = KWhiteColor;
        _tView.tableHeaderView = self.headerView;
//        [_tView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        _tView.estimatedRowHeight= 0;
        _tView.estimatedSectionHeaderHeight= 0;
        _tView.estimatedSectionFooterHeight= 0;
    }
    return _tView;
}

-(SGPageTitleView*)pageTitleView{
    if(!_pageTitleView){
        NSArray *titleArr = @[@"全部", @"文章", @"视频", @"小视频", @"问答", @"美食"];
        _pageTitleView=[SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, KScreenWidth, 44.0) delegate:self titleNames:titleArr configure:self.configure];
        
    }
    return _pageTitleView;
}
-(SGPageContentScrollView*)pageContentScrollView{
    if(!_pageContentScrollView){
        _pageContentScrollView=[[SGPageContentScrollView alloc] initWithFrame:CGRectMake(0, 44.0, KScreenWidth, KScreenHeight-K_TopHeight-44) parentVC:self childVCs:self.childArr];
    _pageContentScrollView.delegatePageContentScrollView= self;
        //因为分页管理器也是属于UIScrollView，也需要交给管理器来统一管理
        UIScrollView *sView = [_pageContentScrollView valueForKey:@"_scrollView"];
        [self.kjManager addMainRelevancyPageView:sView];
    }
    return _pageContentScrollView;
}
- (void)dealloc {
    /**需要销毁管理器的观察者*/
    if (self.kjManager) {
        [self.kjManager kjMultipleManagerDealloc];
    }
}
@end
